package cn.school.mybatis.servlet;


import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.service.StudentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/doStudentInfoServlet")
public class doStudentInfoServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = 1;
        int pageSize = 15;

        try {
            pageNo = Integer.parseInt(req.getParameter("pageNo"));
        } catch (Exception e) {
            pageNo = 1;
        }
        try {
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        } catch (Exception e) {
            pageSize = 15;
        }


        //条件查询
        String sname = req.getParameter("sname");
        String telephone = req.getParameter("telephone");
        sname = sname == null ? "" : sname;
        telephone = telephone == null ? "" : telephone;


        Map<String, Object> map = new HashMap<>();

        map.put("sname", sname);
        map.put("telephone", telephone);

        List<Student> lists = StudentService.STUDENT_SERVICE.search(pageNo, pageSize, map);
        double pageTotal = StudentService.STUDENT_SERVICE.findTotal(map);
        int pageNum = (int) Math.ceil(pageTotal / pageSize);

//        req.setAttribute("pageNo",pageNo);
//        req.setAttribute("pageSize",pageSize);
//        req.setAttribute("pageTotal",(int) pageTotal);
//        req.setAttribute("pageNum",pageNum);
//        req.setAttribute("sname",sname);
//        req.setAttribute("telephone",telephone);
//        req.setAttribute("stuLists",lists);
//        req.getRequestDispatcher("/AdminLTEdemo01/s_student/s_studentInfo.jsp").forward(req,resp);

        //定义一个map对象存放json数据
        Map<String, Object> stuPage = new HashMap<>();
        stuPage.put("pageNo", pageNo);
        stuPage.put("pageSize", pageSize);
        stuPage.put("pageTotal", (int) pageTotal);
        stuPage.put("pageNum", pageNum);
        stuPage.put("sname", sname);
        stuPage.put("telephone", telephone);
        stuPage.put("stuDatas", lists);
        String stuPageJSON = JSON.toJSONString(stuPage,SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullNumberAsZero);
        System.out.println(stuPageJSON);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().print(stuPageJSON);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
